package com.example.lhsama.controller;

import com.example.lhsama.domain.Orders;
import com.example.lhsama.domain.Role;
import com.example.lhsama.domain.User;
import com.example.lhsama.service.RoleService;
import com.example.lhsama.service.UserService;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping("/list")
    public String list(Model model){
        List<User> users = userService.list();
        model.addAttribute("users",users);
        return "/pages/userList";
    }

    @RequestMapping("/insert")
    public String insert(Model model){
        List<Role> roles = roleService.list();
        model.addAttribute("roles",roles);

        return "/pages/userInsert";
    }

    //根据用户已拥有的角色自动check对应复选框，待完成
    @RequestMapping("/update/{id}")
    public String update(Model model, @PathVariable Integer id){
        User user = userService.getById(id);
        List<Role> roles = roleService.list();
        List<Role> roles1 = userService.getRolesList(id);
        model.addAttribute("roles",roles);
        model.addAttribute("checkRole",roles1);
        model.addAttribute("user",user);

        return "/pages/userUpdate";
    }

    @RequestMapping("/save")
    public String save(@RequestParam(value = "roleIds",required = false)Integer[] roleIds, User user){
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        if (user.getPassword().length()==0)
            user.setPassword(null);
        if (user!=null&&user.getPassword()!=null){
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }
        if(user.getId()==null){
            userService.save(user);
        }else {
            userService.updateById(user);
        }
        if (roleIds.length > 0){
            if (user.getId()!=null)
            userService.deleteRoleByUid(user.getId());
            for (Integer roleId : roleIds) {
                roleService.saveUidRid(user.getId(),roleId);
            }
        }
        return "redirect:/user/list";
    }

    @RequestMapping("/close/{id}")
    public String close(@PathVariable Integer id){
        User user = userService.getById(id);
        if (user.getEnable()==1){
            user.setEnable(0);
            userService.updateById(user);
        }else{
            user.setDeleted(1);
            userService.updateById(user);
        }

        return "redirect:/user/list";
    }

    @RequestMapping("/open/{id}")
    public String open(@PathVariable Integer id){
        User user = userService.getById(id);
        user.setEnable(1);
        userService.updateById(user);
        return "redirect:/user/list";
    }

    @RequestMapping("/recovery/{id}")
    public String recovery(@PathVariable Integer id){
        User user = userService.getById(id);
        user.setDeleted(0);
        userService.updateById(user);
        return "redirect:/user/list";
    }



}
